package entity

import (
	"github.com/google/uuid"
	"gorm.io/gorm"
)

type Project struct {
	ID          string     `json:"id" gorm:"column:id"`
	Name        string     `json:"name" gorm:"column:name"`
	Description string     `json:"description" gorm:"column:description"`
	Workflows   []Workflow `json:"workflows" gorm:"-"`
}

func (p *Project) TableName() string {
	return "projects"
}

func (p *Project) BeforeCreate(tx *gorm.DB) (err error) {
	p.ID = uuid.NewString()
	return
}

func (p *Project) AfterFind(tx *gorm.DB) (err error) {
	workflows := make([]Workflow, 0)
	err = tx.Where("project_id = ?", p.ID).Find(&workflows).Error
	if err != nil {
		return err
	}
	p.Workflows = workflows
	return nil
}
